//
//  ReadFlipView.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/14.
//

import UIKit

class ReadFlipView: UIView {
    
    lazy var simuPageBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("翻頁", for: .normal)
        btn.titleLabel?.textAlignment = .center
        btn.titleLabel?.font = .wn_font(name: .bold, size: 13)
        btn.setImage(UIImage(named: "read_setting_bottom_sel"), for: .normal)
        btn.rx.tap.bind { [weak self] in
            self?.changeFlip("Page")
        }.disposed(by: rx.disposeBag)
        return btn
    }()

    lazy var coverPageBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("覆蓋", for: .normal)
        btn.titleLabel?.textAlignment = .center
        btn.titleLabel?.font = .wn_font(name: .bold, size: 13)
        btn.setImage(UIImage(named: "read_setting_bottom_sel"), for: .normal)
        btn.rx.tap.bind { [weak self] in
            self?.changeFlip("Cover")
        }.disposed(by: rx.disposeBag)
        return btn
    }()
    
    lazy var flipPageBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("翻閱", for: .normal)
        btn.titleLabel?.textAlignment = .center
        btn.titleLabel?.font = .wn_font(name: .bold, size: 13)
        btn.setImage(UIImage(named: "read_setting_bottom_sel"), for: .normal)
        btn.rx.tap.bind { [weak self] in
            self?.changeFlip("Flip")
        }.disposed(by: rx.disposeBag)
        return btn
    }()
    
    lazy var scrollPageBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("捲動", for: .normal)
        btn.titleLabel?.textAlignment = .center
        btn.titleLabel?.font = .wn_font(name: .bold, size: 13)
        btn.setImage(UIImage(named: "read_setting_bottom_sel"), for: .normal)
        btn.rx.tap.bind { [weak self] in
            self?.changeFlip("Scroll")
        }.disposed(by: rx.disposeBag)
        return btn
    }()
        
    var flipItem = "Page"
    
    var changeFlipClosure: ReturnClosure?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(simuPageBtn)
        addSubview(scrollPageBtn)
        addSubview(flipPageBtn)
        addSubview(coverPageBtn)
        simuPageBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(scaleWidth(36))
        }
        scrollPageBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-scaleWidth(36))
        }
        flipPageBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.centerX.equalToSuperview().offset(-scaleWidth(36))
        }
        coverPageBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.centerX.equalToSuperview().offset(scaleWidth(36))
        }
        scrollPageBtn.layoutButton(style: .bottom, imageTitleSpace: 4)
        simuPageBtn.layoutButton(style: .bottom, imageTitleSpace: 4)
        flipPageBtn.layoutButton(style: .bottom, imageTitleSpace: 4)
        coverPageBtn.layoutButton(style: .bottom, imageTitleSpace: 4)
        wn_cornerWithBorder(22)
    }
    
    func darkModeConfig() {
        backgroundColor = .init(named: isDark() ? "black_1B1B1B" : "white_F9F8F8")
        flipItem = getReadConfigItem("flip") as? String ?? "Page"
        
        scrollPageBtn.setTitleColor(showColor("Scroll"), for: .normal)
        scrollPageBtn.imageView?.isHidden = flipItem != "Scroll"
        simuPageBtn.setTitleColor(showColor("Page"), for: .normal)
        simuPageBtn.imageView?.isHidden = flipItem != "Page"
        flipPageBtn.setTitleColor(showColor("Flip"), for: .normal)
        flipPageBtn.imageView?.isHidden = flipItem != "Flip"
        coverPageBtn.setTitleColor(showColor("Cover"), for: .normal)
        coverPageBtn.imageView?.isHidden = flipItem != "Cover"
    }
    
    func changeFlip(_ item: String) {
        flipItem = item
        saveReadConfigItem("flip", flipItem)
        darkModeConfig()
        changeFlipClosure?()
    }
    
    func showColor(_ item: String) -> UIColor {
        return UIColor.init(named: flipItem == item ? "red_FE6390" : (isDark() ? "gray_666666" : "black_333333")) ?? .black
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
